/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "Hogshead.Common.h"
#include <tinyxml.h>
namespace Hogshead
{
	namespace Core
	{
		class ParameterField
		{
			public:
				
				enum Type{Unknown = 0, Bool, Char, Short, Int, UnsignedInt, Float, Double, String, Vector2, Vector3, Vector4, Color};
				static const char* _type_names[13];
				ParameterField();
				~ParameterField();
				void registerVariable(Type, const Hogshead::Common::String&, void*);
				bool getType(const Hogshead::Common::String&, Type&) const;
				bool getValue(const Hogshead::Common::String&, void*) const;
				template<typename T> bool setValue(const Hogshead::Common::String& name, const T& value)
				{
					Pair<Type, void*>* p;
					if(_data->tryGetValue(name, p))
					{
						if(!isType<T>(p->first))
							return false;
						
						*((T*)p->second) = value;
						return true;
					}
					return false;
				}
				
				// reads in attribute variabels from an XML Node
				void readXMLAttributes(TiXmlElement* node);
				Hogshead::Common::String writeXMLAttributes();

				const Vector<Hogshead::Common::String>& getVariableNames() const;
				const char* getTypeName(Type) const;

				friend PrintStream& operator<<(PrintStream& in_stream, const ParameterField&);

			private:

				HashTable<Hogshead::Common::String, Pair<Type, void*>* >* _data;
				Vector<Hogshead::Common::String>* _variable_names;


				// assignment
				template<typename T> static void set(void* dest, void* source)
				{
					*(T*)dest = *(T*)source;
				}

				// holy template function specialization batman

				template<typename T> bool isType(Type t)
				{
					return false;
				}

				template<> bool isType<bool>(Type t)
				{
					return t == Bool;
				}

				template<> bool isType<char>(Type t)
				{
					return t == Char;
				}

				template<> bool isType<short>(Type t)
				{
					return t == Short;
				}

				template<> bool isType<int>(Type t)
				{
					return t == Int;
				}
				
				template<> bool isType<unsigned int>(Type t)
				{
					return t == UnsignedInt;
				}

				template<> bool isType<float>(Type t)
				{
					return t == Float;
				}
				
				template<> bool isType<double>(Type t)
				{
					return t == Double;
				}

				template<> bool isType<Hogshead::Common::String>(Type t)
				{
					return t == String;
				}
				
				template<> bool isType<Hogshead::Common::Vector2>(Type t)
				{
					return t == Vector2;
				}

				template<> bool isType<Hogshead::Common::Vector3>(Type t)
				{
					return t == Vector3;
				}

				template<> bool isType<Hogshead::Common::Vector4>(Type t)
				{
					return t == Vector4;
				}

				template<> bool isType<Hogshead::Common::Color>(Type t)
				{
					return t == Color;
				}

		};
	}
}